package com.lineage.server.serverpackets;

import com.lineage.config.ConfigOther;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.utils.Random;
import com.lineage.server.world.World;
import java.util.concurrent.atomic.AtomicInteger;

public class S_UseArrowSkill extends ServerBasePacket {
  private static final AtomicInteger _sequentialNumber = new AtomicInteger(0);
  
  private byte[] _byte;
  
  private int _actid;
  
  public S_UseArrowSkill(L1Character cha, int targetobj, int spellgfx, int targetx, int targety, int dmg, int actId) {
    this._byte = null;
    this._actid = 1;
    switch (cha.getTempCharGfx()) {
      case 3860:
        this._actid = 21;
        break;
      case 2716:
        this._actid = 19;
        break;
      case 5127:
      case 10649:
      case 11714:
        this._actid = 30;
        break;
      case 10566:
        this._actid = 18;
        break;
    } 
    writeC(30);
    if (Random.nextInt(100) < 25)
      this._actid = SpecialActid(cha); 
    if (actId > 1)
      this._actid = actId; 
    writeC(this._actid);
    writeD(cha.getId());
    L1Object obj = World.get().findObject(targetobj);
    if (obj instanceof L1NpcInstance) {
      L1NpcInstance npc = (L1NpcInstance)obj;
      int[] bow_GFX_Arrow = ConfigOther.AtkNo;
      int array[], n = (array = bow_GFX_Arrow).length;
      int i = 0;
      while (i < n) {
        int gfx = array[i];
        if (npc.getTempCharGfx() == gfx)
          targetobj = 0; 
        i++;
      } 
      switch (npc.getTempCharGfx()) {
        case 2544:
        case 10913:
          targetobj = 0;
          break;
      } 
    } 
    if (obj instanceof L1PcInstance) {
      L1PcInstance pc1 = (L1PcInstance)obj;
      int[] atkpc = ConfigOther.AtkNo_pc;
      int array[], n = (array = atkpc).length;
      int i = 0;
      while (i < n) {
        int gfx = array[i];
        if (pc1.getTempCharGfx() == gfx)
          targetobj = 0; 
        i++;
      } 
    } 
    writeD(targetobj);
    L1Object target = World.get().findObject(targetobj);
    if (target instanceof L1PcInstance) {
      L1PcInstance tg = (L1PcInstance)target;
      if (tg.getTempCharGfx() == 23353 || tg.getTempCharGfx() == 23759)
        dmg = 0; 
    } 
    if (dmg > 0) {
      writeH(10);
    } else {
      writeH(0);
    } 
    writeC(cha.getHeading());
    writeD(_sequentialNumber.incrementAndGet());
    writeH(spellgfx);
    writeC(0);
    writeH(cha.getX());
    writeH(cha.getY());
    writeH(targetx);
    writeH(targety);
    writeD(0);
    writeC(0);
  }
  
  public S_UseArrowSkill(L1Character cha, int spellgfx, int targetx, int targety, int actId) {
    this._byte = null;
    this._actid = 1;
    switch (cha.getTempCharGfx()) {
      case 3860:
        this._actid = 21;
        break;
      case 2716:
        this._actid = 19;
        break;
      case 5127:
      case 10649:
      case 11714:
        this._actid = 30;
        break;
      case 10566:
        this._actid = 18;
        break;
    } 
    writeC(30);
    if (Random.nextInt(100) < 25)
      this._actid = SpecialActid(cha); 
    if (actId > 1)
      this._actid = actId; 
    writeC(this._actid);
    writeD(cha.getId());
    writeD(0);
    writeH(0);
    writeC(cha.getHeading());
    writeD(_sequentialNumber.incrementAndGet());
    writeH(spellgfx);
    writeC(0);
    writeH(cha.getX());
    writeH(cha.getY());
    writeH(targetx);
    writeH(targety);
    writeD(0);
    writeC(0);
  }
  
  public S_UseArrowSkill(L1Character cha) {
    this._byte = null;
    this._actid = 1;
    switch (cha.getTempCharGfx()) {
      case 3860:
        this._actid = 21;
        break;
      case 2716:
        this._actid = 19;
        break;
      case 5127:
      case 10649:
      case 11714:
        this._actid = 30;
        break;
      case 10566:
        this._actid = 18;
        break;
    } 
    writeC(30);
    if (Random.nextInt(100) < 25)
      this._actid = SpecialActid(cha); 
    writeC(this._actid);
    writeD(cha.getId());
    writeD(0);
    writeH(1);
    writeC(cha.getHeading());
    writeD(_sequentialNumber.incrementAndGet());
    writeH(2510);
    writeC(0);
    writeH(cha.getX());
    writeH(cha.getY());
    writeH(0);
    writeH(0);
    writeD(0);
    writeC(0);
  }
  
  public S_UseArrowSkill(L1Character cha, int targetobj, int spellgfx, int x, int y, int dmg) {
    this._byte = null;
    this._actid = 1;
    int aid = 1;
    switch (cha.getTempCharGfx()) {
      case 3860:
        aid = 21;
        break;
      case 2716:
        aid = 19;
        break;
    } 
    writeC(30);
    writeC(aid);
    writeD(cha.getId());
    writeD(targetobj);
    if (dmg > 0) {
      writeH(10);
    } else {
      writeH(0);
    } 
    writeC(cha.getHeading());
    writeD(338);
    writeH(spellgfx);
    writeC(127);
    writeH(cha.getX());
    writeH(cha.getY());
    writeH(x);
    writeH(y);
    writeD(0);
    writeC(0);
  }
  
  private int SpecialActid(L1Character cha) {
    if (cha instanceof L1PcInstance) {
      L1PcInstance pc = (L1PcInstance)cha;
      int tempgfxid = cha.getTempCharGfx();
      L1ItemInstance weapon = pc.getWeapon();
      if (weapon == null)
        return this._actid; 
      int weapontype = weapon.getItem().getType1();
      if (tempgfxid == 13723 && weapontype == 20)
        this._actid = 75; 
      if (tempgfxid == 13725 && weapontype == 20)
        this._actid = 75; 
      if (tempgfxid == 13218)
        this._actid = 30; 
      if (tempgfxid == 12314)
        this._actid = 30; 
    } 
    return this._actid;
  }
  
  public byte[] getContent() {
    if (this._byte == null) {
      this._byte = this._bao.toByteArray();
    } else {
      int seq = 0;
      synchronized (this) {
        seq = _sequentialNumber.incrementAndGet();
      } 
      this._byte[13] = (byte)(seq & 0xFF);
      this._byte[14] = (byte)(seq >> 8 & 0xFF);
      this._byte[15] = (byte)(seq >> 16 & 0xFF);
      this._byte[16] = (byte)(seq >> 24 & 0xFF);
    } 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
